package com.dinree.api;

import android.content.Context;

import com.base.library.http.ApiSchedulers;
import com.base.library.http.ApiUtil;
import com.base.library.http.BaseApi;
import com.dinree.bean.OrderBean;
import com.dinree.bean.UserBean;

import io.reactivex.Observable;


/**
 * Created by ChengZhi on 2016/5/24.
 */
public class UserApi extends BaseApi {
    protected final ApiService service;
    private static volatile UserApi instance;

    private UserApi(Context context) {
        service = getRetrofit(context).create(ApiService.class);
    }

    public static UserApi getInstance(Context context) {
        if (instance == null) {
            synchronized (UserApi.class) {
                if (instance == null) {
                    instance = new UserApi(context.getApplicationContext());
                }
            }
        }
        return instance;
    }

    public Observable<UserBean> register(String type, String phone, String pwd, String code) {
        return service.register(type, phone, pwd, code).flatMap(ApiUtil::flatResult).compose(new ApiSchedulers());
    }

    public Observable<UserBean> third_login(String name, String openid, String sex, String img) {
        return service.third_login(name, openid, sex, img).flatMap(ApiUtil::flatResult).compose(new ApiSchedulers());
    }

    public Observable<UserBean> login(String phone, String code) {
        return service.login(phone, code).flatMap(ApiUtil::flatResult).compose(new ApiSchedulers());
    }

    public Observable<String> logout(String id, String token) {
        return service.logout(id, token).flatMap(ApiUtil::flatResult).compose(new ApiSchedulers());
    }

    public Observable<OrderBean> get_dealer_state(String id, String token) {
        return service.get_dealer_state(id, token).flatMap(ApiUtil::flatResult).compose(new ApiSchedulers());
    }

    public Observable<UserBean> get_basic_info(String id, String token) {
        return service.get_basic_info(id, token).flatMap(ApiUtil::flatResult).compose(new ApiSchedulers());
    }

    public Observable<OrderBean> wait_comment(String id, String token) {
        return service.wait_comment(id, token).flatMap(ApiUtil::flatResult).compose(new ApiSchedulers());
    }

    public Observable<String> change_phone(String id, String token, String phone, String use_captcha) {
        return service.change_phone(id, token, phone, use_captcha).flatMap(ApiUtil::flatResult).compose(new ApiSchedulers());
    }

    public Observable<String> verify_password(String id, String token, String password) {
        return service.verify_password(id, token, password).flatMap(ApiUtil::flatResult).compose(new ApiSchedulers());
    }

    public Observable<String> verify_old_phone(String id, String token, String phone, String use_captcha) {
        return service.verify_old_phone(id, token, phone, use_captcha).flatMap(ApiUtil::flatResult).compose(new ApiSchedulers());
    }

    public Observable<String> pw_set(String id, String token, String phone, String use_captcha, String password) {
        return service.pw_set(id, token, phone, use_captcha, password).flatMap(ApiUtil::flatResult).compose(new ApiSchedulers());
    }

    public Observable<String> dealer_verify(String id, String token, String name,
                                            String corpname,
                                            String corp_address,
                                            String corp_license) {
        return service.dealer_verify(id, token, name, corpname, corp_address, corp_license).flatMap(ApiUtil::flatResult).compose(new ApiSchedulers());
    }

    public Observable<String> saveBasicInfo(String id, String token, String nickname,
                                            String avatar,
                                            String mobile,
                                            String email,
                                            String gender,
                                            String district,
                                            String birthday,
                                            String name,
                                            String corpname,
                                            String corp_address,
                                            String corp_license,
                                            String referee_name,
                                            String referee_mobile,
                                            String referee_corpname,
                                            String referee_corpaddress) {
        return service.saveBasicInfo(id, token, nickname,
                avatar,
                mobile,
                email,
                gender,
                district,
                birthday,
                name,
                corpname,
                corp_address,
                corp_license,
                referee_name,
                referee_mobile,
                referee_corpname,
                referee_corpaddress).flatMap(ApiUtil::flatResult).compose(new ApiSchedulers());
    }
}
